package com.wwcom.darteagle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.wwcom.darteagle.fragment.PlayerHistoryMatchListFragment;
import com.wwcom.darteagle.fragment.PlayerHistoryMatchListFragment.OnMatchSelectedListener;
import com.wwcom.darteagle.fragment.PlayerHistoryPlayerListFragment;
import com.wwcom.darteagle.fragment.StatsDetailsFragment;
import com.wwcom.darteagle.fragment.PlayerHistoryPlayerListFragment.OnPlayerSelectedListener;
import com.wwcom.darteagle.model.MatchInfo;
import com.wwcom.darteagle.model.Player;


public class PlayerHistoryActivity extends FragmentActivity
	implements OnPlayerSelectedListener, OnMatchSelectedListener {
	
	private long playerId;
	
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	    //layout
        setContentView(R.layout.playerhistory);
	                                   
        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of a Fragment
            PlayerHistoryPlayerListFragment playerListFragment = new PlayerHistoryPlayerListFragment();
            
            
            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, playerListFragment).commit();
        }        
        
        playerId= -1;
                                
    }

    //OnPlayerSelectedListener
    public void onPlayerSelected(Player player) {
        // Create an instance of a Fragment
        PlayerHistoryMatchListFragment matchListFragment = new PlayerHistoryMatchListFragment();
        Bundle args= new Bundle();
        args.putString(PlayerHistoryMatchListFragment.PLAYER_NAME, player.getName());
        
        matchListFragment.setArguments(args);
        playerId= player.getId();
        Log.d("PlayerHistoryActivity","PlayerId: "+playerId);
        
        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, matchListFragment)
                .addToBackStack(null)
                .commit();
    	
    }
    
    //OnMatchSelectedListener
    public void onMatchSelected(MatchInfo mi) {
    	
		Intent i= new Intent(this,MatchStatsActivity.class);
		i.putExtra(StatsDetailsFragment.LEG_IND, -1);
		i.putExtra(StatsDetailsFragment.SET_IND, -1);
    	if (mi == null) {
    		Log.d("PlayerHistoryActivity","MatchID: -2");
    		i.putExtra(StatsDetailsFragment.MATCH_ID, -2L); //player overall stats
            Log.d("PlayerHistoryActivity","PlayerId: "+playerId);
    		i.putExtra(StatsDetailsFragment.PLAYER_ID, playerId);
    	} else
    		i.putExtra(StatsDetailsFragment.MATCH_ID, mi.getId());
	    startActivity(i);    	
    }
}